<?php
class DestinationController extends Apps_Controller_Action_Default {
	//COUNTRY ARTICLE
	public function generalInformationAction() {
		$cId = intval($this->_request->getParam('id'));
	
		$validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'country', 'field' => 'id'));
		if(!$cId  || $validator->isValid($cId)) {
			$this->_redirect('');
		}
	
		$articleModel = new M_Article();
		$listArticle  = $articleModel->listCountryArticle($cId, M_DbTable_Article::COUNTRY_ARTICLE_TYPE_GENERAL_INFORMATION);
		$this->setPagination($listArticle, $this->_request, 5);
	
		$countryModel = new M_Country();
		$this->view->country = $countryModel->getCountryById($cId)->query()->fetch();
		$this->view->type    = M_DbTable_Article::COUNTRY_ARTICLE_TYPE_GENERAL_INFORMATION;
	}
	
	public function travelTipsAction() {
		$cId = intval($this->_request->getParam('id'));
	
		$validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'country', 'field' => 'id'));
		if(!$cId  || $validator->isValid($cId)) {
			$this->_redirect('');
		}
	
		$articleModel = new M_Article();
		$listArticle  = $articleModel->listCountryArticle($cId, M_DbTable_Article::COUNTRY_ARTICLE_TYPE_TRAVEL_TIPS);
		$this->setPagination($listArticle, $this->_request, 5);
	
		$countryModel = new M_Country();
		$this->view->country = $countryModel->getCountryById($cId)->query()->fetch();
		$this->view->type    = M_DbTable_Article::COUNTRY_ARTICLE_TYPE_TRAVEL_TIPS;
		$this->render('general-information');
	}
	
	public function travelNewsAction() {
		$cId = intval($this->_request->getParam('id'));
	
		$validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'country', 'field' => 'id'));
		if(!$cId  || $validator->isValid($cId)) {
			$this->_redirect('');
		}
	
		$articleModel = new M_Article();
		$listArticle  = $articleModel->listCountryArticle($cId, M_DbTable_Article::COUNTRY_ARTICLE_TYPE_TRAVEL_NEWS);
		$this->setPagination($listArticle, $this->_request, 5);
	
		$countryModel = new M_Country();
		$this->view->country = $countryModel->getCountryById($cId)->query()->fetch();
		$this->view->type    = M_DbTable_Article::COUNTRY_ARTICLE_TYPE_TRAVEL_NEWS;
		$this->render('general-information');
	}
	
	public function detailCountryAction() {
		$id = intval($this->_request->getParam('id'));
		$t  = intval($this->_request->getParam('t'));
	
		$articleModel = new M_Article();
		$article = $articleModel->getCountryArticleById($id)->query()->fetch();
		if(!$id || !$t || !$article) {
			$this->goBack();
		} else {
			$this->view->article = $article;
			$this->view->otherLink = $articleModel->listOtherCountryArticle($article['country_id'], $t, $id)->query()->fetchAll();
		}
		$type = array(
				M_DbTable_Article::COUNTRY_ARTICLE_TYPE_GENERAL_INFORMATION   	=> 'General_Information',
				M_DbTable_Article::COUNTRY_ARTICLE_TYPE_TRAVEL_TIPS	            => 'Travel_Tips',
				M_DbTable_Article::COUNTRY_ARTICLE_TYPE_TRAVEL_NEWS				=> 'Travel_News'
		);
		$this->view->labelType = isset($type[$t]) ? $type[$t] : $type[M_DbTable_Article::COUNTRY_ARTICLE_TYPE_GENERAL_INFORMATION];
		$this->view->t = $t;
	}
	
	//TRAVEL DESTINATION
	//VietNam => list; Other => 1 article
	public function travelDestinationAction() {
		$countryId = intval($this->_request->getParam('id'));
	
		$validator = new Zend_Validate_Db_NoRecordExists(array('table' => 'country', 'field' => 'id'));
		if(!$countryId  || $validator->isValid($countryId)) {
			$this->_redirect('');
		}
		
		$articleModel = new M_Article();
		$countryModel = new M_Country();
		$regionModel  = new M_RegionDestination();
		$areaModel    = new M_AreaDestination();
		
		//Check is VIETNAM
		if($countryId == 1) {
			$area = $areaModel->listareaByCountryId($countryId)->query()->fetchAll();
			foreach($area as $key => $value) {
				$region = $regionModel->listRegionByAreaId($value['id'])->query()->fetchAll();
				$area[$key]['list_region'] = $region;		
			}			
			$this->view->area = $area;
		} else {
			$article = $articleModel->getArticleTravelDestinationByCountryId($countryId)->query()->fetch();
			if($article) {
				$this->view->article = $article;
				$this->view->otherLink = $articleModel->listOtherArticleTravelDestinationByCountryId($countryId, $article['id'])->query()->fetchAll();
			}
		}
// 				Zend_Debug::dump($this->view->otherLink);die();
		$this->view->country = $countryModel->getCountryById($countryId)->query()->fetch();
	}
	
	public function detailRegionAction() {
		$id = intval($this->_request->getParam('id'));
		$t  = intval($this->_request->getParam('t'));
		 
		$articleModel = new M_Article();
		$article = $articleModel->getRegionArticleById($id)->query()->fetch();
		if(!$id || !$t || !$article) {
			$this->_redirect('');
		} else {
			$this->view->article   = $article;
			$this->view->otherLink = $articleModel->listOtherRegionArticle($article['region_id'], $article['country_id'], $t, $id)->query()->fetchAll();
		}
		$type = array(
				M_DbTable_Article::REGION_ARTICLE_TYPE_TRAVEL_DESTINATION => 'Travel_Destination'
		);
		$this->view->labelType = $type[$t];
		$this->view->t = $t;
	}
}